<template>
  <div :class="className" :style="{ height: height, width: width }" />
</template>

<script>
import echarts from 'echarts'
require('echarts/theme/macarons') // echarts theme
import resize from './mixins/resize'
import { IsPC } from '@/utils'

export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '420px'
    },
    autoResize: {
      type: Boolean,
      default: true
    },
    chartData: {
      type: Object,
      required: true
    }
  },
  data() {
    return {
      chart: null
    }
  },
  watch: {
    chartData: {
      deep: true,
      handler(val) {
        this.setOptions(val)
      }
    }
  },
  mounted() {
    this.$nextTick(() => {
      this.initChart()
    })
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    initChart() {
      this.chart = echarts.init(this.$el, 'macarons')
      this.setOptions(this.chartData)
    },
    setOptions({ seriesData, xAxisTitle, legendTitle, legendTitleSelected, yAxis, grid } = {}) {
      this.chart.setOption({
        grid: {
          ...(grid || {
            left: 10,
            right: 10,
            bottom: 20,
            top: 100
          }),
          containLabel: true
        },
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            type: 'cross'
          },
          padding: [5, 10]
        },
        toolbox: {
          feature: {
            saveAsImage: {}
          }
        },
        xAxis: {
          type: 'category',
          data: xAxisTitle,
          boundaryGap: true,
          // splitLine: {
          //   show: true
          // },
          axisLabel: {
            interval: 0,
            rotate: 30
          },
          axisTick: {
            alignWithLabel: true
          },
          axisPointer: {
            type: 'shadow'
          }
        },
        yAxis: yAxis || [
          {
            type: 'value',
            name: '总量',
            splitArea: {
              show: false
            },
            splitLine: {
              show: false
            },
            axisLabel: {}
          },
          {
            type: 'value',
            name: '占比',
            // min: 0,
            // max: 100,
            // interval: 10,
            splitLine: {
              show: false
            },
            splitArea: {
              show: false
            },
            axisLabel: {
              formatter: '{value} %'
            }
          }
        ],
        legend: {
          data: legendTitle,
          selected: legendTitleSelected || {}
        },
        series: seriesData,
        dataZoom: IsPC() ? [] : [
          {
            id: 'dataZoomX',
            type: 'inside',
            xAxisIndex: [0],
            filterMode: 'none',
            start: 0,
            end: 15
          }
        ]
      })
    }
  }
}
</script>
